package com.blog.controller.admin;

import com.blog.Constant.JwtClaimsConstant;
import com.blog.dto.UserDTO;
import com.blog.dto.UserEditPwd;
import com.blog.dto.UserLoginDTO;
import com.blog.entity.User;
import com.blog.properties.JwtProperties;
import com.blog.result.Result;
import com.blog.service.UserService;
import com.blog.utils.JwtUtil;
import com.blog.vo.UserVO;
import com.blog.vo.UserLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController("adminUserController")
@RequestMapping("/admin/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    @GetMapping("/list")
    public Result<List<UserVO>> list() {
        log.info("获取所有用户信息.....");
        List<UserVO> userList = userService.getAll();
        return Result.success(userList);
    }

    @GetMapping("/list/{name}")
    public Result<List<UserVO>> listByName(@PathVariable("name") String name) {
        log.info("根据名称模糊查询获取所有用户信息.....");
        List<UserVO> userList = userService.listByName(name);
        return Result.success(userList);
    }

    @GetMapping("/list/finalByID/{id}")
    public Result<UserVO> listById(@PathVariable("id") Long id) {
        log.info("根据用户ID查询获取用户信息.....");
        UserVO userVO = userService.listById(id);
        return Result.success(userVO);
    }

    @PostMapping("/Add")
    public Result AddUser(@RequestBody UserDTO userDTO) {
        log.info("添加用户信息.....{}",userDTO);
        userService.AddUser(userDTO);
        return Result.success();
    }

    @PutMapping("/Edit")
    public Result EditUser(@RequestBody UserDTO userDTO) {
        log.info("修改用户信息.....{}",userDTO);
        userService.EditUser(userDTO);
        return Result.success();
    }

    @PutMapping("/EditPwd")
    public Result EditPwd(@RequestBody UserEditPwd userEditPwd) {
        log.info("修改用户密码.....{}",userEditPwd);
        boolean status = userService.EditUserPwd(userEditPwd);
        if (status) return Result.success("密码修改成功...");
        return  Result.fail("账号密码错误...");
    }

    @DeleteMapping ("/DeleteById/{uid}")
    public Result DeleteUser(@PathVariable("uid") Long id) {
        log.info("根据ID删除用户信息.....{}",id);
        userService.DeleteUser(id);
        return Result.success();
    }
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {

        log.info("用户登录：{}", userLoginDTO);

        //用户登录
        User user = userService.login(userLoginDTO);

        //登陆成功后：用户不为空，为微信用户生成JWT令牌
        UserLoginVO userLoginVO = null;
        if (user != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put(JwtClaimsConstant.USER_ID, userLoginDTO.getUserId());
            claims.put(JwtClaimsConstant.NAME, user.getName());
            String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);

            //定义要返回的VO对象
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user,userVO);
            userLoginVO = UserLoginVO.builder()
                    .userVO(userVO)
                    .token(token)
                    .build();
        }
        //返回这个用户的VO对象，携带token
        return Result.success(userLoginVO);
    }
}
